package com.ruoyi.device.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.device.service.IStatisticsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * WiFi统计数据Controller
 * 
 * @author ruoyi
 * @date 2025-09-12
 */
@RestController
@RequestMapping("/statistics")
public class StatisticsController extends BaseController
{
    @Autowired
    private IStatisticsService statisticsService;

    /**
     * 获取统计概览数据
     */
    @RequiresPermissions("device:statistics:list")
    @GetMapping("/overview")
    public AjaxResult getOverview(@RequestParam(value = "startDate", required = false) String startDate,
                                  @RequestParam(value = "endDate", required = false) String endDate,
                                  @RequestParam(value = "deviceName", required = false) String deviceName,
                                  @RequestParam(value = "locationName", required = false) String locationName) 
    {
        Map<String, Object> overview = statisticsService.getOverviewStatistics(startDate, endDate, deviceName, locationName);
        return success(overview);
    }

    /**
     * 获取设备详细统计列表
     */
    @RequiresPermissions("device:statistics:list")
    @GetMapping("/device/list")
    public TableDataInfo getDeviceStatistics(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "startDate", required = false) String startDate,
                                             @RequestParam(value = "endDate", required = false) String endDate,
                                             @RequestParam(value = "deviceName", required = false) String deviceName,
                                             @RequestParam(value = "locationName", required = false) String locationName)
    {
        startPage();
        List<Map<String, Object>> list = statisticsService.getDeviceStatistics(startDate, endDate, deviceName, locationName);
        return getDataTable(list);
    }

    /**
     * 获取连接趋势图表数据
     */
    @RequiresPermissions("device:statistics:list")
    @GetMapping("/chart/connection")
    public AjaxResult getConnectionChart(@RequestParam(value = "startDate", required = false) String startDate,
                                        @RequestParam(value = "endDate", required = false) String endDate,
                                        @RequestParam(value = "deviceId", required = false) String deviceId)
    {
        Map<String, Object> chartData = statisticsService.getConnectionChartData(startDate, endDate, deviceId);
        return success(chartData);
    }

    /**
     * 获取设备状态分布数据
     */
    @RequiresPermissions("device:statistics:list")
    @GetMapping("/chart/status")
    public AjaxResult getStatusChart()
    {
        Map<String, Object> statusData = statisticsService.getDeviceStatusData();
        return success(statusData);
    }

    /**
     * 获取地域分布数据
     */
    @RequiresPermissions("device:statistics:list")
    @GetMapping("/chart/region")
    public AjaxResult getRegionChart(@RequestParam(value = "startDate", required = false) String startDate,
                                    @RequestParam(value = "endDate", required = false) String endDate)
    {
        Map<String, Object> regionData = statisticsService.getRegionChartData(startDate, endDate);
        return success(regionData);
    }

    /**
     * 获取时段分析数据
     */
    @RequiresPermissions("device:statistics:list")
    @GetMapping("/chart/hour")
    public AjaxResult getHourChart(@RequestParam(value = "startDate", required = false) String startDate,
                                  @RequestParam(value = "endDate", required = false) String endDate)
    {
        Map<String, Object> hourData = statisticsService.getHourChartData(startDate, endDate);
        return success(hourData);
    }

    /**
     * 获取设备详细信息
     */
    @RequiresPermissions("device:statistics:list")
    @GetMapping("/device/detail")
    public AjaxResult getDeviceDetail(@RequestParam("deviceId") String deviceId,
                                     @RequestParam(value = "startDate", required = false) String startDate,
                                     @RequestParam(value = "endDate", required = false) String endDate)
    {
        Map<String, Object> detail = statisticsService.getDeviceDetailInfo(deviceId, startDate, endDate);
        return success(detail);
    }

    /**
     * 导出统计报表
     */
    @RequiresPermissions("device:statistics:export")
    @Log(title = "WiFi统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                      @RequestParam(value = "startDate", required = false) String startDate,
                      @RequestParam(value = "endDate", required = false) String endDate,
                      @RequestParam(value = "deviceName", required = false) String deviceName,
                      @RequestParam(value = "locationName", required = false) String locationName)
    {
        statisticsService.exportStatistics(response, startDate, endDate, deviceName, locationName);
    }
} 